/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.directedit;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.draw2d.geometry.Rectangle;


/**
 * Manage a list of Label Controlers.
 * A label controler is used to associate a Label to model properties in a lightweight way.
 * The manager is used
 * 
 * @author dumoulin
 */
public class LabelControlerManager {

	/**
	 * The list of controlers.
	 */
	protected List<LabelControler> controlers = new ArrayList<LabelControler>();
	
	/**
	 * 
	 * 
	 * @param label 
	 * @param propertyAccessor 
	 */

	public void createLabelControler( Label label, PropertyAccessor propertyAccessor) {
		controlers.add( new LabelControler(label, propertyAccessor) );
	}
	
	/**
	 * 
	 * 
	 * @param label 
	 */
	public void removeLabelControlerFor( Label label ) {
		if( label == null ) {
			return;
		}
		
		Iterator iter = controlers.iterator();
		while(iter.hasNext())
		{
			ILabelControler labelCtrl = (ILabelControler)iter.next();
			if( label.equals( labelCtrl.getLabel() ) ) {
				iter.remove();
			}
		}
	}
	
	/**
	 * Get the controler under the specified point.
	 * Walk the registered labels and return the one under the specified position.
	 * Return null in no label is found.
	 * 
	 * @param p The mouse position to check
	 * 
	 * @return The controler of the label under the position or null if none is found.
	 */
	public ILabelControler getControlerAt(Point p) {
		Iterator iter = controlers.iterator();
		while(iter.hasNext())
		{
			ILabelControler labelCtrl = (ILabelControler) iter.next();
			if( labelContainsPoint( labelCtrl.getLabel(), p) ) {
				return labelCtrl;
			}
		}
		return null;
	}

	/**
	 * Get the label at the specified position.
	 * Walk the registered labels and return the one under the specified position.
	 * Return null in no label is found.
	 * 
	 * @param p The mouse position to check
	 * 
	 * @return The label under the position or null if none is found.
	 */
	public IFigure getLabelAt( Point p )
	{
		Iterator iter = controlers.iterator();
		while(iter.hasNext())
		{
			ILabelControler labelCtrl = (ILabelControler)iter.next();
			if( labelContainsPoint( labelCtrl.getLabel(), p) ) {
				return labelCtrl.getLabel();
			}
		}
		return null;
	}
	
	/**
	 * Check if the label contains the specified point.
	 * 
	 * @param label 
	 * @param Point The point to check
	 * @param pt 
	 * @param Label The figure to check
	 * 
	 * @return boolean True if the label contains the specified point.
	 */

	protected boolean labelContainsPoint(IFigure label, Point pt) {
		Rectangle rc;

			rc = new PrecisionRectangle(label.getBounds());
			label.translateToAbsolute(rc);
			if (rc.contains(pt)) {
				return true;
			}
		return false;
	}

		

}
